<template>
  <div class="query-page">
    <header class="header">
      <button class="back-btn" @click="$router.push('/back')">
        <span>‹</span>
        返回
      </button>
      <h1>音乐查询</h1>
    </header>
    
    <div class="content">
      <div class="search-section">
        <div class="input-group">
          <input 
            type="text" 
            v-model="queryText" 
            placeholder="请输入歌曲名称"
            class="search-input"
            @keypress.enter="musicQuery"
          />
        </div>
        
        <div class="button-group">
          <button class="btn btn-clear" @click="clearQuery">
            <span class="btn-icon">🗑️</span>
            清空
          </button>
          <button class="btn btn-search" @click="musicQuery" :disabled="loading">
            <span class="btn-icon" v-if="!loading">🔍</span>
            <div class="spinner-small" v-else></div>
            {{ loading ? '查询中...' : '查询' }}
          </button>
        </div>
      </div>
      
      <div class="result-section">
        <!-- <div class="section-title">查询结果</div> -->
        <div class="result-area">
          <div v-if="loading" class="loading">
            <div class="spinner"></div>
            <p>正在查询中...</p>
          </div>
          <div v-else-if="resultText" class="result-content">
            <pre>{{ resultText }}</pre>
          </div>
          <div v-else class="empty-state">
            <div class="empty-icon">🎵</div>
            <p>请输入查询内容并点击查询按钮</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { apiService } from '../utils/http'

export default {
  name: 'MusicQuery',
  data() {
    return {
      queryText: '',
      resultText: '',
      loading: false
    }
  },
  methods: {
    async musicQuery() {
      if (!this.queryText.trim()) {
        alert('请输入查询内容')
        return
      }
      
      this.loading = true
      this.resultText = ''
      
      try {
        const res = await apiService.post('/MusicQuery', {music: this.queryText})
        this.resultText = res.data.data
      } catch (error) {
        console.error('查询失败:', error)
        this.resultText = '查询失败，请检查网络连接后重试'
      } finally {
        this.loading = false
      }
    },
    
    clearQuery() {
      this.queryText = ''
      this.resultText = ''
    }
  }
}
</script>

<style scoped>
.query-page {
  min-height: 100vh;
  background: #f8f9fa;
}

.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  color: white;
}

.back-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: background 0.3s ease;
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.header h1 {
  font-size: 20px;
  font-weight: 600;
}

.content {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.search-section {
  background: white;
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.input-group {
  margin-bottom: 16px;
}

.search-input {
  width: 100%;
  padding: 16px;
  border: 1px solid #e0e0e0;
  border-radius: 12px;
  font-size: 16px;
  transition: border-color 0.3s ease;
}

.search-input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1);
}

.search-input::placeholder {
  color: #999;
}

.button-group {
  display: flex;
  gap: 12px;
}

.btn {
  flex: 1;
  padding: 16px 20px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none !important;
}

.btn-clear {
  background: #f8f9fa;
  color: #666;
  border: 1px solid #e0e0e0;
}

.btn-clear:hover {
  background: #e9ecef;
  transform: translateY(-1px);
}

.btn-search {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.btn-search:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.spinner-small {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.result-section {
  background: white;
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  flex: 1;
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 16px;
  padding-left: 4px;
}

.result-area {
  min-height: 300px;
  border: 1px solid #e0e0e0;
  border-radius: 12px;
  background: #fafafa;
  overflow: auto;
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #666;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #e0e0e0;
  border-top: 3px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.result-content {
  padding: 20px;
}

.result-content pre {
  white-space: pre-wrap;
  word-wrap: break-word;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  color: #333;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #666;
  text-align: center;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.5;
}
</style>
